#!/bin/bash
# 最新版本的注意可能gcc版本低有问题；3.6.8以后需要openssl1.1.1以上
version=3.8.1
# openssl version
ssl_version=1.1.1
soft_dir=/opt/softs
[ -d ${soft_dir} ] || mkdir -p ${soft_dir}

yum install -y epel-release wget gcc perl libffi-devel zlib-devel pcre-devel sqlite-devel openssl-devel
yum -y groupinstall 'Development Tools'

function install_openssl(){
  cd ${soft_dir}
  ssl_dir=/usr/local/openssl${ssl_version}
  [ -f openssl-${ssl_version}.tar.gz ] || wget https://www.openssl.org/source/old/${ssl_version}/openssl-${ssl_version}.tar.gz
  tar -zxvf openssl-${ssl_version}.tar.gz
  [ -d openssl-${ssl_version} ] && rm -rf openssl-${ssl_version}
  cd openssl-${ssl_version}
  ./config --prefix=${ssl_dir}
  make && make install
  export PATH=${ssl_dir}/bin:$PATH
  echo "${ssl_dir}/lib" > /etc/ld.so.conf.d/openssl-${ssl_version}.conf
  ldconfig -v
}

function install_python(){
  cd ${soft_dir}
  install_dir=/usr/local/python${version}
  ssl_dir=/usr/local/openssl${ssl_version}
  [ -f Python-${version}.tgz ] || wget https://www.python.org/ftp/python/${version}/Python-${version}.tgz
  [ -d Python-${version} ] || tar -zxvf Python-${version}.tgz
  cd Python-${version}
  export LDFLAGS="-L${ssl_dir}/lib"
  export CFLAGS="-I${ssl_dir}/include"
  export PKG_CONFIG_PATH="${ssl_dir}/lib/pkgconfig/"
  echo 'SSL=${ssl_dir}
_ssl _ssl.c \
       -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
       -L$(SSL)/lib64 -lssl -lcrypto' >> Modules/Setup.dist
  ./configure  --prefix=${install_dir} --enable-optimizations --with-openssl=${ssl_dir}
  make && make install
#  [ `cat /root/.bashrc|grep python3|wc -l` -eq 0 ] && echo "export PATH=${install_dir}/bin:$PATH" >> /root/.bashrc
  [ -L /usr/bin/python3 ] && rm -f /usr/bin/python3
  [ -L /usr/bin/pip3 ] && rm -f /usr/bin/pip3
  ln -s ${install_dir}/bin/python3 /usr/bin/python3
  ln -s ${install_dir}/bin/pip3 /usr/bin/pip3
}

install_openssl
install_python

python3 -c "import sys;print(sys.version)"


